package controllers.power.admin;

import controllers.BaseController;
import controllers.auth.Secure;
import helper.GlobalConfig;
import models.admin.AdminBusinessOperate;
import models.admin.AdminMenu;
import models.admin.AdminRole;
import models.admin.AdminRoleOperate;
import models.constants.AvailableStatus;
import org.apache.commons.lang.StringUtils;
import play.Logger;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;
import util.common.ConvertUtil;

import java.util.*;

/**
 * 角色管理
 * Created by youliangcheng on 17/4/8.
 */
@With(BaseController.class)
public class AdminRoleCtrl extends Controller {
    public static void loadRoleList(Integer pageNumber,
                                    Long id,
                                    String code,
                                    String name) {

        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String, Object> searchMap = new HashMap<>();
        if (id != null)
            searchMap.put("id", id);

        if (StringUtils.isNotBlank(code))
            searchMap.put("code", "%" + code + "%");

        if (StringUtils.isNotBlank(name))
            searchMap.put("name", "%" + name + "%");

        JPAExtPaginator<AdminRole> resultPage = AdminRole.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber, code);
        render();
    }

    //禁用角色
    public static void freezeRole(long id, Integer pageNumber) {
        pageNumber = pageNumber == null ? 1 : pageNumber;
        AdminRole adminRole = AdminRole.findById(id);
        //判断重复操作
        if(adminRole.status == AvailableStatus.FREEZE){
            flash.put("msg", "该角色已经被禁用,无需重复操作!");
            redirect("/power/admin/AdminRoleCtrl/loadRoleList/" + pageNumber);
        }
        //判断是否正在被使用
        if(AdminRole.checkIsUsed(id)){
            flash.put("msg", "该角色正在被使用,不能禁用!");
            redirect("/power/admin/AdminRoleCtrl/loadRoleList/" + pageNumber);
        }

        adminRole.status = AvailableStatus.FREEZE;
        Secure.setBaseInfo(adminRole);
        adminRole.save();
        flash.put("msg", "禁用成功");
        redirect("/power/admin/AdminRoleCtrl/loadRoleList/" + pageNumber);
    }


    //启用角色
    public static void availableRole(long id, Integer pageNumber) {
        pageNumber = pageNumber == null ? 1 : pageNumber;
        AdminRole adminRole = AdminRole.findById(id);
        //判断重复操作
        if(adminRole.status == AvailableStatus.AVAILABLE){
            flash.put("msg", "该角色已经被启用,无需重复操作!");
            redirect("/power/admin/AdminRoleCtrl/loadRoleList/" + pageNumber);
        }
        adminRole.status = AvailableStatus.AVAILABLE;
        Secure.setBaseInfo(adminRole);
        adminRole.save();
        flash.put("msg", "启用成功");
        redirect("/power/admin/AdminRoleCtrl/loadRoleList/" + pageNumber);
    }


    public static void loadRoleDetail(long id, Integer pageNumber) {
        AdminRole adminRole = new AdminRole();
        //adminMenuList
        List<AdminMenu> adminMenuList = AdminMenu.findAll();
        if (id > 0) {
            adminRole = AdminRole.findById(id);

        }
        render(id, adminRole, adminMenuList, pageNumber);

    }

    //加载操作数据
    public static void loadRoleOperates(long id) {
        Map<String, Object> resultMap = new HashMap<>();
        AdminRole adminRole = new AdminRole();
        List<AdminRoleOperate> roleOperateList = new ArrayList<>();
        List<Map<String, Object>> businessList = new ArrayList<>();
        if (id > 0) {
            adminRole = AdminRole.findById(id);
            roleOperateList = AdminRoleOperate.loadByRoleId(id);
        }
        if (ConvertUtil.toLong(adminRole.menuId) > 0) {
            businessList = AdminMenu.loadMenuBusiness(adminRole.menuId);
        }

        resultMap.put("adminRole", adminRole);
        resultMap.put("businessList", businessList);
        resultMap.put("roleOperateList", roleOperateList);
        renderJSON(ConvertUtil.toJson(resultMap));

    }

    /**
     * 根据菜单id加载业务菜单
     *
     * @param menuId
     */
    public static void loadMenuBusienss(long menuId) {

        renderJSON(AdminMenu.loadMenuBusiness(menuId));
    }

    public static void saveDetail(AdminRole adminRole, String operateIds, Integer pageNumber) {

        if (adminRole.id != null && adminRole.id > 0) { // 新建
            Secure.setBaseInfo(adminRole);
            adminRole.update();
            //清空操作
            AdminRoleOperate.deleteByRoleId(adminRole.id);
        } else { // 修改
            adminRole.loadKey();
            adminRole.status = AvailableStatus.AVAILABLE;
            Secure.setBaseInfo(adminRole);
            adminRole.save();

        }


        if (operateIds.length() > 0) {
            String[] operates = operateIds.split(",");
            for (String operateId : operates) {
                AdminRoleOperate operate = new AdminRoleOperate();
                AdminBusinessOperate adminBusinessOperate = AdminBusinessOperate.findById(ConvertUtil.toLong(operateId));
                if(adminBusinessOperate != null){
                    operate.adminRole = adminRole;
                    operate.adminBusinessOperate = adminBusinessOperate;
                    operate.createAt = new Date();
                    operate.save();
                }
            }

        }

        redirect("/power/admin/AdminRoleCtrl/loadRoleDetail/" + adminRole.id + "/" + pageNumber);
    }


}
